Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
es6-promisify
Advanced tools
The es6-promisify package is a utility that converts callback-based functions to return Promises, making it easier to work with asynchronous code in a more modern and readable way.
Promisify a single callback-based function
This feature allows you to convert a single callback-based function, such as `fs.readFile`, into a function that returns a Promise. This makes it easier to handle asynchronous operations using modern async/await syntax or Promise chaining.
const promisify = require('es6-promisify');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);
readFileAsync('example.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));
Promisify an entire object of callback-based functions
This feature allows you to promisify all functions within an object, such as the `fs` module, so that all of its methods return Promises. This is useful for working with modules that have multiple callback-based methods.
const promisify = require('es6-promisify');
const fs = require('fs');
const fsAsync = promisify(fs);
fsAsync.readFile('example.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));
The `util.promisify` function is a built-in Node.js utility that provides similar functionality to es6-promisify. It converts callback-based functions to return Promises. Since it is built into Node.js, it does not require an additional package installation. However, it is only available in Node.js versions 8.0.0 and above.
Bluebird is a fully-featured Promise library that includes a `promisify` method. It offers more advanced features and better performance compared to es6-promisify. Bluebird is a good choice if you need additional Promise utilities and optimizations.
Pify is a lightweight utility that promisifies callback-style functions. It is similar to es6-promisify but offers more customization options, such as filtering which methods to promisify and handling multiple arguments. Pify is a good alternative if you need more control over the promisification process.
Converts callback-based functions to Promise-based functions.
Install with npm
npm install --save es6-promisify
"use strict";
// Declare variables
var promisify = require("es6-promisify"),
fs = require("fs"),
// Convert the stat function
stat = promisify(fs.stat);
// Now usable as a promise!
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
console.error("Yikes!", err);
});
"use strict";
// Declare variables
var promisify = require("es6-promisify"),
fs = require("fs"),
stat;
// Convert the stat function, with a custom callback
stat = promisify(fs.stat, function (err, result) {
if (err) {
console.error(err);
return this.reject("Could not stat file");
}
this.resolve(result);
});
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
// err = "Could not stat file"
});
"use strict";
// Declare variables
var promisify = require("es6-promisify"),
redis = require("redis").createClient(6379, "localhost"),
// Create a promise-based version of send_command
client = promisify(redis.send_command.bind(redis));
// Send commands to redis and get a promise back
client("ping", []).then(function (pong) {
console.log("Got", pong);
}).catch(function (err) {
console.error("Unexpected error", err);
}).then(function () {
redis.quit();
});
Test with nodeunit
$ npm test
Published under the MIT License.
FAQs
Converts callback-based functions to ES6 Promises
We found that es6-promisify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.